package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ProjectContent;
import net.hn.hnms.biz.project.mapper.ProjectContentMapper;
import net.hn.hnms.biz.project.request.ProjectContentReq;
import net.hn.hnms.biz.project.service.ProjectContentService;
import net.hn.hnms.biz.project.vo.ProjectContentVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectContentServiceImpl implements ProjectContentService {
    @Autowired
    private ProjectContentMapper projectContentMapper;

    @Override
    public int insert(ProjectContentReq projectContentReq) {
        ProjectContent insertObj = new ProjectContent();
        BeanUtil.copyProperties(projectContentReq, insertObj);
        return projectContentMapper.insert(insertObj);
    }
    @Override
    public void updateDeleteByIds(List<String> idList) {
        projectContentMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(ProjectContentReq projectContentReq) {
        ProjectContent updateObj = new ProjectContent();
        BeanUtil.copyProperties(projectContentReq, updateObj);
        int updateCount = projectContentMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ProjectContent selectDetailById(String id) {
        ProjectContent projectContent = projectContentMapper.selectById(id);
        if (projectContent == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return projectContent;
    }

    @Override
    public ResponsePage<ProjectContentVo> selectPageList(ProjectContentReq projectContentReq, PageQuery pageQuery) {
        Page<ProjectContentVo> page = projectContentMapper.selectPageList(pageQuery.build(),projectContentReq);
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ProjectContent> buildQuery(ProjectContentReq req){
        return new LambdaQueryWrapper<ProjectContent>()
                .like(StrUtil.isNotBlank(req.getProjectContentCode()), ProjectContent::getProjectContentCode, req.getProjectContentCode());
    }

}
